var db = require('../db/commonDB')
module.exports = {
    //创建订单
    createOrder: function (orders, cb) {
        var sql = `insert into orders(user_id,orders_goods_img,orders_goods_title,orders_goods_price,orders_goods_count,orders_goods_size) value(?,?,?,?,?,?)`
        var params = [ orders.user_id,orders.orders_goods_img,orders.orders_goods_title,orders.orders_goods_price,orders.orders_goods_count,orders.orders_goods_size]
        db(sql, params, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results) //创建订单成功
                } else {
                    cb(new Error('创建订单失败！'))
                }
            }
        })
    },
    //查看订单
    getUserOrder: function (orders, cb) {
        var sql = `select * from orders where user_id = ? and orders_pay_state = 0`
        var params = orders.user_id
        db(sql, params, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('查询订单失败！'))
                }
            }
        })
    },
    changePersonalOrder:function(orders,cb){
        var sql = `update orders set orders_payed_view_state = 1 where orders_id = ${orders.orders_id} and user_id = ${orders.user_id}`
        var params = [orders.user_id,orders.orders_id]
        db(sql, params, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('修改订单状态失败！'))
                }
            }
        })
    },
    //删除订单
    // delOrder: function (orders, cb) {
    //     var sql = `delete from orders where user_id = ?`
    //     var params = orders.user_id
    //     db(sql, params, function (err, results) {
    //         if (err) {
    //             cb(err)
    //         } else {
    //             if (results.affectedRows > 0) {
    //                 cb(null, results)
    //             } else {
    //                 cb(new Error('删除订单失败！'))
    //             }
    //         }
    //     })
    // },
    //根据订单id删除订单
    delOrderById:function (orders, cb) {
        var sql = `delete from orders where orders_id = ? and orders_pay_state = 0`
        var params = orders.orders_id
        db(sql, params, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('删除订单失败！'))
                }
            }
        })
    },
    //查看订单详情
    getOrderDetails: function(order_details,cb){
        var sql = `select * from order_details where orders_id = ?`
        var params = order_details.orders_id
        db(sql, params, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('查看订单失败！'))
                }
            }
        })
    },
    //查看已支付订单
    getPayedUserOrder:function(orders,cb){
        var sql = `select * from orders where user_id = ? and orders_pay_state = 1 and orders_payed_view_state = 0`
        var params = orders.user_id
        db(sql, params, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.length > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('查看已支付订单失败，该用户可能无订单！'))
                }
            }
        })
    },
    //修改订单状态
    changeOrder: function(orders,cb){
        var sql = `update orders set orders_pay_state = ?,orders_total = ? where orders_id = ${orders.orders_id} and user_id = ${orders.user_id}`
        var params = [orders.orders_pay_state,orders.orders_total,orders.orders_id,orders.user_id]
        db(sql, params, function (err, results) {
            if (err) {
                cb(err)
            } else {
                if (results.affectedRows > 0) {
                    cb(null, results)
                } else {
                    cb(new Error('修改订单状态失败！'))
                }
            }
        })
    }
}